<?php
/**
  * 网仓接口
  */
define('IN_ECS', true);
require('../includes/init.php');
@ini_set('display_errors',        2);

require(ROOT_PATH . 'includes/lib_wangcang.php');
require_once(ROOT_PATH . 'includes/lib_order.php');
require_once(ROOT_PATH . 'includes/cls_json.php');
require_once(ROOT_PATH . 'languages/zh_cn/wangcang.php');
$json = new JSON();
$appkey = !empty($_POST['appkey'])?$_POST['appkey']:false;
$user = !empty($_POST['user'])?$_POST['user']:false;
$pass = !empty($_POST['pass'])?$_POST['pass']:false;
$action = !empty($_POST['act']) ? trim($_POST['act']) : 'default';
$vdata = !empty($_POST['vdata'])?($json->decode($_POST['vdata'], true)):false;

if($appkey != 'aab4fea140' || $user != 'wangcang' || $pass != 'wangcang123'){
    wangcangError(101);
}

/* 修改订单状态 */
if ($action == 'setOrderStatus' && $vdata['status'] && $vdata['orderSn']) {
    if(empty($vdata['orderSn'])){
        wangcangError(201);
    } elseif (empty($vdata['status'])) {
        wangcangError(202);
    } elseif (empty($_LANG['status'][$vdata['status']])){
        wangcangError(203);
    } else {
        if($orderInfo = order_info(false, intval($vdata['orderSn']))){
            $data['err'] = '0';
            $data['errMsg'] = $_LANG['err'][$data['err']];
            $arr['wangcangstatus'] = "$vdata[status]";
            if($vdata['status'] == '600' && $orderInfo['pay_id'] == '1'){
                $arr['order_status'] = "5";
                $arr['shipping_status'] = "2";
                $arr['pay_status'] = "2";
            }
            update_order($orderInfo['order_id'], array($arr));
            echo $json->encode($orderInfo, false);
            exit;
        }else{
            wangcangError(204);
        }
    }
}

/* 导入入库单 */
elseif ($action == 'pushStockIn') {
    if(empty($vdata['count'])){
        wangcangError(301);
    }
    if(empty($vdata['stockIn']) || !is_array($vdata['stockIn'])){
        wangcangError(302);
    }elseif (empty($vdata['stockIn']['skus']) || !is_array($vdata['stockIn']['skus'])){
        wangcangError(302);
    }elseif (empty($vdata['stockIn']['stockInSn'])){
        wangcangError(303);
    }elseif (empty($vdata['stockIn']['addTime'])){
        wangcangError(304);
    }elseif (empty($vdata['stockIn']['stockId'])){
        wangcangError(305);
    }elseif (empty($vdata['stockIn']['source'])){
        wangcangError(310);
    }elseif (empty($_LANG['stockInSource'][$vdata['stockIn']['source']])){
        wangcangError(312);
    }else{
        if($db->getOne("SELECT stockInSn FROM " . $ecs->table('wcStockIn') . " WHERE stockInSn = '" . $vdata['stockIn']['stockInSn'] . "'")) wangcangError(309);
        $stockIn['stockInSn'] = $vdata['stockIn']['stockInSn'];
        $stockIn['addTime'] = intval($vdata['stockIn']['addTime']);
        $stockIn['stockId'] = intval($vdata['stockIn']['stockId']);
        $stockIn['source'] = intval($vdata['stockIn']['source']);
        if($stockIn['source'] == 2){
            $stockIn['orderSn'] = $vdata['stockIn']['orderSn'];
            if(!$db->getOne("SELECT order_sn FROM " . $ecs->table('order_info') . " WHERE order_sn = '" . $stockIn['orderSn'] . "'")) wangcangError(311, $stockIn['orderSn']);
        }
        foreach ($vdata['stockIn']['skus'] as $key => $value) {
            if(!$value['sku']) wangcangError(306);
            if(!$value['qty']) wangcangError(307);
            if(!$db->getOne("SELECT goods_sku FROM " . $ecs->table('goods_stock') . " WHERE goods_sku = '$value[sku]'")) wangcangError(308, $value['sku']);
            $stockIn['skus'][$key]['stockInSn'] = $stockIn['stockInSn'];
            $stockIn['skus'][$key]['sku'] = $value['sku'];
            $stockIn['skus'][$key]['qty'] = $value['qty'];
        }
        $db->autoExecute($ecs->table('wcStockIn'), $stockIn);
        foreach ($stockIn['skus'] as $key => $value) {
            $db->autoExecute($ecs->table('wcStockInSku'), $value);
        }
        wangcangError('0');
    }
}
/* 导入出库单 */
elseif ($action == 'pushStockOut') {
    if(empty($vdata['count'])){
        wangcangError(501);
    }
    if(empty($vdata['stockOut']) || !is_array($vdata['stockOut'])){
        wangcangError(502);
    }elseif (empty($vdata['stockOut']['skus']) || !is_array($vdata['stockOut']['skus'])){
        wangcangError(502);
    }elseif (empty($vdata['stockOut']['stockOutSn'])){
        wangcangError(503);
    }elseif (empty($vdata['stockOut']['addTime'])){
        wangcangError(504);
    }elseif (empty($vdata['stockOut']['stockId'])){
        wangcangError(505);
    }elseif (empty($vdata['stockOut']['source'])){
        wangcangError(510);
    }elseif (empty($_LANG['stockOutSource'][$vdata['stockOut']['source']])){
        wangcangError(512);
    }else{
        if($db->getOne("SELECT stockOutSn FROM " . $ecs->table('wcStockOut') . " WHERE stockOutSn = '" . $vdata['stockOut']['stockOutSn'] . "'")) wangcangError(509);
        $stockOut['stockOutSn'] = $vdata['stockOut']['stockOutSn'];
        $stockOut['addTime'] = intval($vdata['stockOut']['addTime']);
        $stockOut['stockId'] = intval($vdata['stockOut']['stockId']);
        $stockOut['source'] = intval($vdata['stockOut']['source']);
        if($stockOut['source'] == 2){
            $stockOut['orderSn'] = $vdata['stockOut']['orderSn'];
            if(!$db->getOne("SELECT order_sn FROM " . $ecs->table('order_info') . " WHERE order_sn = '" . $stockOut['orderSn'] . "'")) wangcangError(511, $stockOut['orderSn']);
        }
        foreach ($vdata['stockOut']['skus'] as $key => $value) {
            if(!$value['sku']) wangcangError(506);
            if(!$value['qty']) wangcangError(507);
            if(!$db->getOne("SELECT goods_sku FROM " . $ecs->table('goods_stock') . " WHERE goods_sku = '$value[sku]'")) wangcangError(508, $value['sku']);
            $stockOut['skus'][$key]['stockOutSn'] = $stockOut['stockOutSn'];
            $stockOut['skus'][$key]['sku'] = $value['sku'];
            $stockOut['skus'][$key]['qty'] = $value['qty'];
        }
        $db->autoExecute($ecs->table('wcStockOut'), $stockOut);
        foreach ($stockOut['skus'] as $key => $value) {
            $db->autoExecute($ecs->table('wcStockOutSku'), $value);
        }
        wangcangError('0');
    }
}
/* 修改条形码信息 */
elseif ($action == 'setSkuToCode') {
    if(empty($vdata['count'])) wangcangError(401);
    if(empty($vdata['codes']) || !is_array($vdata['codes'])) wangcangError(402);
    if($vdata['count'] != count($vdata['codes'])) wangcangError(403);
    foreach ($vdata['codes'] as $key => $value) {
        if(!$value['sku']) wangcangError(404);
        if(!$value['code']) wangcangError(405);
        if(!$db->getOne("SELECT goods_sku FROM " . $ecs->table('goods_stock') . " WHERE goods_sku = '$value[sku]'")) wangcangError(406, intval($value['sku']));
        $skuToCode[$key]['sku'] = intval($value['sku']);
        $skuToCode[$key]['code'] = trim($value['code']);
    }
    foreach ($skuToCode as $key => $value) {
        $db->autoReplace($ecs->table('wcSkuToCode'), array('sku'=>$value['sku'],'code'=>$value['code']), array('code'=>$value['code']));
    }
    wangcangError();
}
else {
    wangcangError(102);
}

function wangcangError($status = '0', $value = ''){
    require(ROOT_PATH . 'languages/zh_cn/wangcang.php');
    $json = new JSON();
    $data['err'] = $status;
    $data['errMsg'] = $_LANG['err'][$status] . ($value?"/$value":'');
    echo $json->encode($data, false);
    exit;
}